package com.vf.admin.utils.sendsms;

import com.baidubce.auth.DefaultBceCredentials;
import com.baidubce.services.sms.SmsClient;
import com.baidubce.services.sms.SmsClientConfiguration;
import com.baidubce.services.sms.model.SendMessageV3Request;
import com.baidubce.services.sms.model.SendMessageV3Response;

import java.util.HashMap;
import java.util.Map;

public class BaiDuSms {

    private static final String ACCESS_KEY_ID = "";
    private static final String SECRET_ACCESS_KEY = "";
    private static final String ENDPOINT = "";

    public static void send(String mobile, String code) {
        SmsClientConfiguration config = new SmsClientConfiguration();
        // 设置HTTP最大连接数为10
        config.setMaxConnections(10);
        // 设置TCP连接超时为5000毫秒
        config.setConnectionTimeoutInMillis(5000);
        // 设置Socket传输数据超时的时间为2000毫秒
        config.setSocketTimeoutInMillis(2000);
        config.setCredentials(new DefaultBceCredentials(ACCESS_KEY_ID,SECRET_ACCESS_KEY));
        config.setEndpoint(ENDPOINT);

        SmsClient client = new SmsClient(config);

        SendMessageV3Request request = new SendMessageV3Request();
        request.setMobile(mobile);
        request.setSignatureId("sms-sign-BfPEHc76280");
        request.setTemplate("sms-tmpl-yYTiRa03383");
        Map<String, String> contentVar = new HashMap<>();
        contentVar.put("number", code);
        request.setContentVar(contentVar);
        SendMessageV3Response response = client.sendMessage(request);
        // 解析请求响应 response.isSuccess()为true 表示成功
        if (response != null && response.isSuccess()) {
            //  submit success
        } else {
            // fail
        }
    }
}
